﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kaiser.Modules.Core.Services
{
    public class BillCodeService
    {
        public static Queue<int> XsddQueue;
        public static Queue<int> XsdQueue;
        public static Queue<int> JhdQueue;
        public static Queue<int> SkdQueue;
        public static Queue<int> FkdQueue;

        public BillCodeService()
        {
            if (XsddQueue == null && XsdQueue==null && JhdQueue==null && SkdQueue==null && FkdQueue==null)
            {
                XsddQueue = new Queue<int>();
                XsdQueue = new Queue<int>();
                SkdQueue = new Queue<int>();
                JhdQueue = new Queue<int>();
                FkdQueue = new Queue<int>();
                for (int i = 1; i < 100; i++)
                {
                    XsddQueue.Enqueue(i);
                    XsdQueue.Enqueue(i);
                    JhdQueue.Enqueue(i);
                    SkdQueue.Enqueue(i);
                    FkdQueue.Enqueue(i);
                }
            }
        }

        public enum BillCodeType
        {
            销售单,
            销售订单,
            进货单,
            收款单, 付款单

        }

        public string GenerationBillCode(BillCodeType billCodeType)
        {
            var yearmonth = DateTime.Now.ToString("yyyyMM");
            switch (billCodeType)
            {
                case BillCodeType.销售单:
                    var a = XsdQueue.Dequeue();
                    return string.Format("XSD{0}{1}", yearmonth, a.ToString(CultureInfo.InvariantCulture).PadLeft(5, '0'));
                case BillCodeType.销售订单:
                    var b = XsddQueue.Dequeue();
                    return string.Format("XSDD{0}{1}", yearmonth, b.ToString(CultureInfo.InvariantCulture).PadLeft(5, '0'));
                case BillCodeType.进货单:
                    var c = JhdQueue.Dequeue();
                    return string.Format("JHD{0}{1}", yearmonth, c.ToString(CultureInfo.InvariantCulture).PadLeft(5, '0'));
                case BillCodeType.收款单:
                    var d = SkdQueue.Dequeue();
                    return string.Format("SKD{0}{1}", yearmonth, d.ToString(CultureInfo.InvariantCulture).PadLeft(5, '0'));
                case BillCodeType.付款单:
                    var e = FkdQueue.Dequeue();
                    return string.Format("FKD{0}{1}", yearmonth, e.ToString(CultureInfo.InvariantCulture).PadLeft(5, '0'));
            }
            throw new ArgumentOutOfRangeException("billCodeType");
        }
    }
}
